package com.general.persistent.config;

import org.apache.commons.dbcp2.BasicDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

/**
 * 参考：https://www.cnblogs.com/zhuxiaojie/p/5836159.html
 * @author SongJiuHua.
 * @date Created on 2018/3/9.
 * @description
 */
@Configuration
@PropertySource("classpath:general-jdbc.properties")
public class DataSourceConfiguration {

    @Value("${general.jdbc.driver}")
    private String driver;
    @Value("${general.jdbc.url}")
    private String url;
    @Value("${general.jdbc.username}")
    private String userName;
    @Value("${general.jdbc.password}")
    private String password;
    @Value("${general.jdbc.maxActive}")
    private String maxActive;
    @Value("${general.jdbc.maxIdle}")
    private Integer maxIdle;
    @Value("${general.jdbc.maxWait}")
    private Long maxWait;


    @Bean("dataSource")
    public BasicDataSource dataSource(){
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName(driver);
        dataSource.setMaxIdle(maxIdle);
        dataSource.setPassword(password);
        dataSource.setUrl(url);
        dataSource.setUsername(userName);
        dataSource.setMaxWaitMillis(maxWait);
        dataSource.setValidationQuery("SELECT 1");
        return dataSource;
    }


}
